package com.cuz.for2024;

public class T63 {
    public static void main(String[] args) {
        int i = new T63().uniquePathsWithObstacles(new int[][]{new int[]{1, 0}});
        System.out.println(i);
    }
    public int uniquePathsWithObstacles(int[][] obstacleGrid) {

        int m = obstacleGrid.length;
        int n = obstacleGrid[0].length;
        if (obstacleGrid[m - 1][n - 1] == 1) {
            return 0;
        }
        int[][] dp = new int[m][n];
        for (int i = 0; i < m; i++) {
            dp[i][0] = obstacleGrid[i][0] == 1 ? 0 : 1;
        }
        for (int i = 0; i < n; i++) {
            dp[0][i] = obstacleGrid[0][i] == 1 ? 0 : 1;
        }

        for (int i = 1; i < m; i++) {
            for (int j = 1; j < n; j++) {
                dp[i][j] = ways(obstacleGrid, dp, i - 1, j)
                        + ways(obstacleGrid, dp, i, j - 1);
            }
        }
        return dp[m - 1][n - 1];
    }

    private int ways(int[][] obstacleGrid, int[][] dp, int i, int j) {
        if (obstacleGrid[i][j] == 1) {
            return 0;
        }
        return dp[i][j];
    }
}
